import Vue from 'vue';
import App from './App.vue';
import store from '@/store/index';
import router, { routerMap, constantRouterMap } from './router/index';
import VueRouter from 'vue-router';
import Element from 'element-ui';
import i18n from '../public/lang/index'; // Internationalization
import '@/styles/element-variables.scss';
import 'normalize.css';
// [dev报表-element]css地址 [开发模式才需要加载]
if (process.env.NODE_ENV === 'development') {
    require('devextreme/dist/css/dx.common.css');
    require('devextreme/dist/css/dx.light.css');
    require('@/theme/index.css');
    require('vxe-table/lib/index.css');
    require('bpmn-jousenzhou/Bpmn.css');
}
import '@/styles/index.scss'; // global css
// import '@/styles/theme/common-theme.scss' // global css
import { VXETable, XEUtils } from 'vxe-table-jousenzhou';
import '@/plugin/error-log'; // error log
import '@/iconfont/iconfont.css'; // iconfont css
import '@/utils/accuracyLoss';
import '@/svg-icon/index'; // icon
import '@/permission'; // permission control
import '@/components/base-message/index'; // vMessage
import '@/components/base-inform'; // vInform
import '@/plugin/global';
import '@/directives/index';
import '@/filter/index';
import './public-path.js';

Vue.use(VXETable);
Vue.prototype.$XEUtils = XEUtils;
Vue.use(Element, {
    size: 'medium',
    i18n: (key: string, value: string) => i18n.t(key, value)
});
Vue.config.devtools = true;
Vue.config.productionTip = false;
let routerInstance: any = null;
let instance: any = null;
const __qiankun__ = (window as any).__POWERED_BY_QIANKUN__; // 判断是否是 qiankun 环境
store.commit('app/SET_QIANKUN', !!__qiankun__);
console.log('是否是 qiankun 环境', __qiankun__);
export async function bootstrap(props) {
    console.log('props', props);
    // 这里是获取到子应用的 propsData
    // 注册主应用下发的组件
    // Vue.use(components);
    // // 把工具函数挂载在vue $mainUtils对象
    // Vue.prototype.$mainUtils = utils;
    // // 把mainEmit函数一一挂载
    // Object.keys(emitFnc).forEach(i => {
    //     Vue.prototype[i] = emitFnc[i]
    // });
    // // 在子应用注册呼机
    // pager.subscribe(v => {
    //     console.log(`监听到子应用${v.from}发来消息：`, v)
    //         // store.dispatch('app/setToken', v.token)
    // })
    // Vue.prototype.$pager = pager;
    // // 在子应用注册官方通信
    // /* actions.onGlobalStateChange((state, prev) => console.log(`子应用subapp-ui监听到来自${state.from}发来消息：`, state, prev)); */
    // Vue.prototype.$actions = actions;
}

export async function mount({ data = {}, routerBase = '/' } = {}) {
    // console.log('mount data', data, ROUTES, routerBase, 'routerMatch', routeMatch(ROUTES, routerBase));

    Object.assign(router, {
        options: {
            base: __qiankun__ ? routerBase : '/', // 需配置一个 前缀路由
            mode: 'history',
            routes: constantRouterMap
        },
        mode: 'history'
    });
    instance = new Vue({
        router: router,
        store,
        i18n,
        render: (h) =>
            h(App, {
                props: { ...data }
            })
    }).$mount('#subRbpApp');
}

export async function unmount() {
    instance.$destroy();
    instance = null;
    routerInstance = null;
}

// 单独开发环境
__qiankun__ || mount();
// export const RouterInstance = routerInstance;
console.log('RouterInstance', router);
